package sgi.client;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;

import sgi.client.SGIPurchaseImageController.Source;
import sgi.details_new.ImageDetails;
import sgi.message.MessageCharge;
import sgi.message.MessageGetTariff;
import sgi.message.MessageGetTariff.tariff;
import sgi.message.MessagePurchaseImage;
import sgi.message.MessagePurchaseSample;

public class SGIPurchaseSampleController extends SGIController {

	private static SGIPurchaseSampleController purchase;
	private static SGIPurchaseSampleUI purchaseUI;
	private int imageid;
	private ImageDetails details;
	private Source src;
	
	public static SGIPurchaseSampleController create(ImageDetails details,Source src) throws IOException
	{
		if(SGIPurchaseSampleController.purchase == null)
		{
			SGIPurchaseSampleController.purchase = new SGIPurchaseSampleController();
			SGIPurchaseSampleController.purchaseUI.setController(purchase);
			SGIPurchaseSampleController.purchase.setImageid(details.getImageid());
			SGIPurchaseSampleController.purchase.setDetails(new ImageDetails(details));
			purchase.src = src;
		}
		
		purchaseUI.setVisible(true);
		return purchase;
	}
	
	private SGIPurchaseSampleController()
	{
		purchaseUI = new SGIPurchaseSampleUI();
	}
	

	public int getImageid() {
		return imageid;
	}

	public void setImageid(int imageid) {
		this.imageid = imageid;
	}
	
	public static void terminate()
	{
		SGIPurchaseSampleController.purchase = null;
		SGIPurchaseSampleController.purchaseUI.setController(null);
		SGIPurchaseSampleController.purchaseUI.setVisible(false);
		SGIPurchaseSampleController.purchaseUI = null;
	}
	
	public void purchase()
	{
		MessagePurchaseSample msg = new MessagePurchaseSample(imageid);
		msg.setDetails(getDetails());
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	void handleMessage(Object msg) {
		SGIPurchaseSampleController.terminate();
		if(((MessagePurchaseSample)msg).isInsufficiantBalance() == true)
		{
			JOptionPane.showMessageDialog(src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Insufficiant balance.");
		}
		else if(((MessagePurchaseSample)msg).isImageAlreadyPurchased() == true )
		{
			JOptionPane.showMessageDialog(src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Image already purchased.");
		}
		else if(((MessagePurchaseSample)msg).isSampleAlreadyPurchased() == true )
		{
			JOptionPane.showMessageDialog(src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Sample already purchased.");
		}
		else
		{
			JOptionPane.showMessageDialog(SGISearchArchiveController.getUI(),"Sample purchased successfuly.");
			new File(System.getProperty("user.home")+"/My Documents/SGIimages"+SGIUserAccountMenuController.getUser().getUsername()).mkdirs();
			byte[] buffer = ((MessagePurchaseSample)msg).getBlobAsBytes();
			InputStream input = new ByteArrayInputStream(buffer);
			
			try {
			  OutputStream output = new FileOutputStream(System.getProperty("user.home")+"\\My Documents\\SGIimages." +SGIUserAccountMenuController.getUser().getUsername()+ "\\Sample"+ "%" + String.valueOf(((MessagePurchaseSample)msg).getImageid()) + "%" + ((MessagePurchaseSample)msg).getDetails().getDate() + "%" + ((MessagePurchaseSample)msg).getDetails().getLocation()  + "%" +".jpeg");
			  try {
			    int bytesRead;
			    while ((bytesRead = input.read(buffer)) != -1) {
			      output.write(buffer, 0, bytesRead);
			    }
			  } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
			    try {
					output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			  }
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
			  try {
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}

		}
	}

	public ImageDetails getDetails() {
		return details;
	}

	public void setDetails(ImageDetails details) {
		this.details = details;
	}

	public Source getSource()
	{
		return src;
	}

}

